package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.CurrentMoney;
import com.fxsh168.server.R;
import com.fxsh168.server.utils.MD5Utils;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class BalanceActivity extends BaseActivity {
    TextView teTiXian;
    private TextView mCurrentMoney;
    private String mPhone;
    private String mPassword;
    private String token;
    private String mCurrentMoneyData;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance);
        mCurrentMoney = (TextView) findViewById(R.id.currentMoney);
        teTiXian = (TextView) findViewById(R.id.teTiXian);
        //参数：phone 手机号
        //token
        //token生成规则:md5(lis+账号+bin+密码+www625kejicom)
        sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        mPassword = sp.getString("mPassword", "");

        String jMPassword = MD5Utils.MD532(mPassword);
        token = MD5Utils.MD532("lis" + mPhone + "bin" + jMPassword + "www625kejicom");
        handler.sendEmptyMessage(0);
        teTiXian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float doubleMoney = Float.parseFloat(mCurrentMoneyData);
//        int intMoney= (int) doubleMoney;
                SharedPreferences.Editor editor = sp.edit();
                editor.putFloat("intMoney", doubleMoney);
                editor.commit();
                startActivity(new Intent(BalanceActivity.this, BalanceMoneyActivity.class));

            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            OkHttpUtils.post().addParams("phone", mPhone).addParams("token", token)
                    .url(Constant.SIJIYUE).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    showToast("连接不上服务器!");
                }

                @Override
                public void onResponse(String response, int id) {
                    if (response != null) {
                        CurrentMoney currentMoney = new Gson().fromJson(response, CurrentMoney.class);
                        String code = currentMoney.getCode();
                        if (code.equals(200 + "")) {
                            mCurrentMoneyData = currentMoney.getData();
                            mCurrentMoney.setText(mCurrentMoneyData);
                        } else if (code.equals(201 + "")) {
                            showToast("非法请求!");
                        }
                    }
                }
            });
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(0);
    }
}
